#!/bin/bash
main() {
    read -p "请输入 机房号、网卡速率、是否为省内：" can
    _cname=$(echo $can | awk '{print $1}')
    _line_speed=$(echo $can | awk '{print $2}')
    _limited_area=$(echo $can | awk '{print $3}')

    {
        # 网络类型
        if [[ "$_nat_type" == "" ]]; then
            _public_ip=$(ip r | grep 'src [0-9.]{7,15}' -Eo | grep -E "src (10\.|100\.|172\.|192.)" -vc)
            if [[ "$_public_ip" != "0" ]]; then
                _nat_type=0
            else
                _nat_type=1
            fi
        fi
    }

    {
        _myip=""
        for _ in {1..3}; do
            _myip=$(curl -fsSL myip.ipip.net | head -n1)
            echo "$_myip" | grep '当前' -cq && break
            sleep 1
        done
        if [[ -z $_myip ]]; then
            echo "获取IP失败"
            exit 1
        fi
    }

    {
        _isp=" "
        if [ ${_limited_area} -eq 1 ];then
            echo -e "\033[32m【1 电信 】 【2 联通】  【3 移动】\033[0m"
            read -p "请输入需要的异网：" _isp
        fi

        _isp_case=$(echo "$_myip" | grep -Eo "移动|电信|联通" | head -1)
        case "$_isp_case" in
        "移动") 
            _isp_en="m" 
            ;;
        "联通") 
            _isp_en="u"
            ;;
        "电信") 
            _isp_en="t" 
            ;;
        *) _isp_en="unknown" ;;
        esac
    }

    {
        # guid 的一部分
        md5sum /usr/local/bin/pinyin 2>/dev/null | grep 67e24f2e80b2d020e008bc1f075db53e -cq || {
            curl -fsSL -o /usr/local/bin/pinyin https://1133.s.kuaicdn.cn:11338/public/project/240718-pinyin/bin/pinyin
            chmod +x /usr/local/bin/pinyin
        }
        _province_zh=$(echo "$_myip" | awk '{print $(NF-2)}')
        _province_en=$(/usr/local/bin/pinyin "$_province_zh")
    }

    {
        # 线路和业务类型
        _provider=bendy_zx  #默认为专线
        _multi_line_speed="$(echo "$_line_speed" | awk '{print $0/8}')"
        if [[ $_line_speed == "" ]]; then
            echo "速率不能为空"
            exit 1
        elif [[ $_line_speed -ge 100 && $_line_speed -lt 1000 ]]; then
            _node_type=pcdn # 速率>100&&<1000
            _provider_id=42
        elif [[ $_line_speed -ge 1000 ]]; then
            _node_type=acdn # 速率>1000
            _provider=bendy_zx
            _provider_id=142
        else
            _node_type=sx86 # 速率<100
            _provider_id=242
        fi
    }

    {
        # 磁盘大小（只要固态）
        disk=$(lsblk -o rota,mountpoint | grep "^\s\s\s0" | grep "disk" | awk '{print $2}')
        _disk_total=0
        for i in $disk ;do
            disk_size_tmp=$(df -m $i | grep "^/dev" | awk '{print $2}')
            _disk_total=$(($_disk_total+$disk_size_tmp))
        done
        
        # 缓存目录(只要固态)
        _disk_path="$(lsblk -o rota,mountpoint| grep "^\s\s\s0" | grep -Eo '/disk/[0-z]{8}' | xargs -I @ echo '@/cache-ks' | xargs -n99 | sed 's@\s/@,/@g')"
    }

    {
        suid=$(cat /data/kuaicdn/labels/suid)
    
        if [ ${suid:0:4}=="ipmi" ];then
            _suid="${suid:5:8}"
        else
            _suid="${suid:0:8}"
        fi 
    }

    {
        # 获取网卡前缀
        _nic_in_like=$(ip r | grep "^default" | head -1 | awk '{print $5}' | awk -F "-" '{print $1"-"$2}')
        
        if [[ "$_nic_in_like" == "" ]]; then
            echo "无法获取网卡"
            exit 1
        fi
    }

    _Guid="bendy_${_province_en}_${_isp_en}_${_node_type}_${_suid}_${_cname}:000"

    {
        # 生成配置文件
        _confb=""
        if [[ $_limited_area != "0" ]]; then
            _confb=$(
                cat <<EOF
--isp=${_isp}
EOF
            )
        fi

        _confa=$(
            cat <<EOF
--provider=${_provider}
--provider_id=${_provider_id}    
--nic_in_like=${_nic_in_like}
--nic_out_like=${_nic_in_like}
--tcp_nic_port=all:21000
--upnp_port=21001
--thread_cnt=3
--tcp_enable=2
--guid=${_Guid}
--nat=${_nat_type}
--multi_line_speed=${_multi_line_speed}
--path=${_disk_path}
--storage_total=${_disk_total}
--limited_area=${_limited_area}
--log_level=1
--log_path=/disk/log
--log_size=100
${_confb}
EOF

        )
    }
    # 打印配置，并写入配置指定目录
    echo "$_confa"
    read -p "是否要存入配置[y/n]： " y_n

    if [ ${y_n} ==  "y" ] ;then
	    mkdir -p /data/kuaicdn/bbiz/ksp2p/config
        echo "$_confa" >/data/kuaicdn/bbiz/ksp2p/config/args.conf
    else
        exit 2
    fi
}

main